﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Configuration;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using Taxes.Model;
using Taxes.BLL;


namespace Taxes.Users
{
    public partial class UserImport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [DirectMethod]
        public void DeleteFile(string fileName, string fileClientName)
        {
            try
            {
                System.IO.File.Delete(fileName);
                //X.Msg.Notify("提示", fileClientName + "已删除!").Show();
            }
            catch (Exception ex)
            {
                //X.Msg.Notify("提示", fileClientName + "删除失败!").Show();
            }
        }
        [DirectMethod]
        public void  Up(string att)
        {
            var path=  System.Configuration.ConfigurationManager.AppSettings["UploadPath"].ToString();
            var users = new List<Taxes.Model.ysys_user>();

            var filename = Server.MapPath( path + "/" + att);
            #region 分析excel

            using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                var workBook = new HSSFWorkbook(stream);
                var enumerator = workBook.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var sheet = (HSSFSheet)enumerator.Current;
                    if (sheet == null) continue;
                    var rowEnumerator = sheet.GetEnumerator();
                    while (rowEnumerator.MoveNext())
                    {
                        var row = (HSSFRow)rowEnumerator.Current;
                        if (row == null)
                            break;
                        var firstCell = (HSSFCell)row.GetCell(0);
                        if (firstCell == null) break;
                        if (firstCell.StringCellValue == "用户名") continue;
                        var user = new Taxes.Model.ysys_user();
                        var cellEnumerator = row.GetEnumerator();
                        while (cellEnumerator.MoveNext())
                        {
                            var cell = cellEnumerator.Current as HSSFCell;
                            if (cell == null) continue;
                            switch (cell.ColumnIndex)
                            {
                                case 0:
                                    user.user_name = cell.StringCellValue;
                                    break;
                                case 1:
                                    user.user_department = cell.StringCellValue;
                                    break;
                                case 2:
                                    user.user_tname = cell.StringCellValue;
                                    break;
                                case 3:
                                    var value = cell.StringCellValue;
                                    user.user_type = value.Trim() == "管理员"?1:0;
                                    break;
                            }
                        }
                        users.Add(user);
                    }
                }
            }
          
            #endregion
            var bll = new Taxes.BLL.ysys_user();
            foreach (var r in users)
            {
                bll.Add(r);
            }
            Response.Redirect("UserList.aspx");
        }
    }
}